package com.zx.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zx.bus.domain.Car;
import com.zx.sys.constast.SysConstast;
import com.zx.sys.domain.User;
import com.zx.sys.service.UserService;
import com.zx.sys.utils.AppFileUtils;
import com.zx.sys.utils.DataGridView;
import com.zx.sys.utils.ResultObj;
import com.zx.sys.vo.UserVo;

@RestController
@RequestMapping("user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("loadAllUser")
	public DataGridView loadAllUser(UserVo userVo){
		return userService.queryAllUser(userVo);
	}
	
	@RequestMapping("addUser")
	public ResultObj addUser(UserVo userVo){
		try {
			userVo.setUserid(null);
			userService.addUser(userVo);
			return new ResultObj(SysConstast.CODE_SUCCESS, SysConstast.ADD_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR, SysConstast.ADD_ERROR);
		}
	}
	
	@RequestMapping("updateUser")
	public ResultObj updateUser(UserVo userVo){
		try {
			userService.updateUser(userVo);
			return new ResultObj(SysConstast.CODE_SUCCESS, SysConstast.UPDATE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR, SysConstast.UPDATE_ERROR);
		}
	}
	
	@RequestMapping("deleteUser")
	public ResultObj deleteUser(UserVo userVo){
		try {
			userService.deleteUser(userVo.getUserid());
			return new ResultObj(SysConstast.CODE_SUCCESS,SysConstast.DELETE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR,SysConstast.DELETE_ERROR);
		}
	}
	
	@RequestMapping("deleteBatchUser")
	public ResultObj deleteBatchUser(Integer[] ids){
		try {
			userService.deleteBatchUser(ids);
			return new ResultObj(SysConstast.CODE_SUCCESS,SysConstast.DELETE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR,SysConstast.DELETE_ERROR);
		}
	}
	
	@RequestMapping("resetUserPwd")
	public ResultObj resetUserPwd(UserVo userVo) {
		try {
			this.userService.resetUserPwd(userVo.getUserid());
			return new ResultObj(SysConstast.CODE_SUCCESS,SysConstast.RESET_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR,SysConstast.RESET_ERROR);
		}
	}
	
	@RequestMapping("initUserRole")
	public DataGridView initUserRole(UserVo userVo){
		return userService.queryUserRole(userVo.getUserid());
	}
	
	@RequestMapping("saveUserRole")
	public ResultObj saveUserRole(UserVo userVo){
		try {
			userService.saveUserRole(userVo);
			return new ResultObj(SysConstast.CODE_SUCCESS,SysConstast.DISPATCH_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR,SysConstast.DISPATCH_ERROR);
		}
	}
	
	@RequestMapping("changePwd")
	public ResultObj changePwd(UserVo userVo){
		try {
			if(userService.changePwd(userVo)){
				return new ResultObj(SysConstast.CODE_SUCCESS, SysConstast.UPDATE_SUCCESS);
			}
			return new ResultObj(SysConstast.CODE_ERROR, SysConstast.UPDATE_ERROR);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR, SysConstast.UPDATE_ERROR);
		}
	}
}
